<!-- 故障类型 故障类型 -->
<template>
  <div>
    <avue-crud
      ref="crud"
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @tree-load="treeLoad"
      @expand-change="expandChange"
    >
      <template slot="menuLeft">
        <el-button type="danger" plain size="small" icon="el-icon-delete" @click="handleDel">删除</el-button>
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.subExportComp.visible = true">导出</el-button>
      </template>
      <template slot="menu" slot-scope="{ row, size, type }">
        <el-button :size="size" :type="type" @click="handleAdd(row)">新增子项</el-button>
      </template>
    </avue-crud>
    <export-comp ref="subExportComp" page-api-url="mes-product/mro/dict/tree" :option="option" :query="query" file-name="故障类型"></export-comp>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { getList, treeGetList, add, update, remove } from '@/api/equipment-manage/faultSubType';
import exportComp from '@/components/export-comp';
export default {
  components: {
    exportComp
  },
  data() {
    return {
      id: '',
      parentId: undefined,
      form: {},
      defaults: {},
      query: {
        dictType: 'EQUIPMENT',
        subType: 'FAULT_TYPE'
      },
      loading: true,
      selectionList: [],
      option: {
        height: 'calc(100vh - 400px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: false,
        // indexLabel: '序号',
        viewBtn: true,
        viewBtnText: '详情',
        viewTitle: '详情',
        excelBtn: true, // 导出
        delBtn: false,
        selection: true,
        dialogClickModal: false,
        labelWidth: 120,
        menuWidth: 300,
        rowKey: 'id',
        rowParentKey: 'parentId',
        tree: true,
        lazy: true, // 懒加载
        column: [
          {
            label: '故障类型代码',
            prop: 'code',
            search: true,
            span: 24,
            // maxlength: 20,
            // showWordLimit: true,
            searchLabelWidth: 120,
            rules: [
              {
                required: true,
                message: '请输入故障类型代码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '故障类型名称',
            prop: 'name',
            search: true,
            span: 24,
            searchLabelWidth: 120,
            rules: [
              {
                required: true,
                message: '请输入故障类型名称',
                trigger: 'blur'
              }
            ]
          }
        ]
      },
      data: []
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        // addBtn: this.vaildData(this.permission.station_add, false),
        // viewBtn: this.vaildData(this.permission.station_view, false),
        // delBtn: this.vaildData(this.permission.station_delete, false),
        // editBtn: this.vaildData(this.permission.station_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    handleAdd(row) {
      this.parentId = row.id;
      this.$refs.crud.rowAdd();
    },
    rowSave(row, done, loading) {
      row.dictType = 'EQUIPMENT';
      row.subType = 'FAULT_TYPE';
      row.parentId = this.parentId;
      add(row).then(
        () => {
          this.onLoad();
          this.$message({
            type: 'success',
            message: '添加故障类型成功!'
          });
          this.parentId = '';
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      row.dictType = 'EQUIPMENT';
      row.subType = 'FAULT_TYPE';
      update(row).then(
        () => {
          this.onLoad();
          this.$message({
            type: 'success',
            message: '编辑故障类型成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    handleDel() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad();
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad();
    },
    searchChange(params, done) {
      this.query = params;
      this.onLoad();
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud && this.$refs.crud.toggleSelection();
    },
    refreshChange() {
      this.onLoad();
    },
    onLoad() {
      this.loading = true;
      treeGetList(Object.assign(this.query)).then((res) => {
        this.data = res.data.data.map((item, index) => {
          item.dataIndex = index + 1;
          return item;
        });
        this.loading = false;
        this.selectionClear();
      });
    },
    treeLoad(tree, treeNode, resolve) {
      const parentId = tree.id;
      this.treeParams.set(parentId, { tree, treeNode, resolve });
      getLazyList(parentId).then((res) => {
        let data = res.data.data;
        resolve(data);
      });
    },
    expandChange(row, expanded) {
      this.handleTreeDataIndex(row, expanded);
    },
    handleTreeDataIndex(row, checkStatus) {
      if (row.children && row.children.length > 0) {
        row.children.map((d, i) => {
          if (checkStatus) {
            d.dataIndex = row.dataIndex + '-' + (i + 1);
          }
        });
      }
    }
  }
};
</script>
